package br.ucb.cm.main.mainview.ui;

import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.JSeparator;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import br.ucb.cm.config.bean.PieceConfig;
import br.ucb.cm.main.common.GeneralConstants.PlayerEnum;
import br.ucb.cm.main.common.GeneralConstants.PlayerTypeEnum;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.border.BevelBorder;

public class Counter extends JPanel {
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_MAX_WIDTH = 182;
	private static final Color YELLOW = new Color(255, 255, 110);
	private static final Color GREEN = new Color(0, 255, 100);
	private static final Color RED = new Color(255, 0, 0);
	
	private JLabel pointsField;
	private PlayerEnum player;
	private PieceCemetery capturedPieces;
	private JPanel content;

	public Counter(PlayerEnum playerType) {
		this.player = playerType; 
		initComponents();
	}

	private void initComponents() {
		content = new JPanel();
		content.setBorder(new LineBorder(new Color(0, 0, 0)));
		content.setPreferredSize(new Dimension(DEFAULT_MAX_WIDTH, 500));
		content.setMaximumSize(new Dimension(DEFAULT_MAX_WIDTH, Short.MAX_VALUE));
		
		setLayout(new BorderLayout(0, 0));
		add(content);
		content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
		
		JLabel lblPlayer = new JLabel(player.getName());
		if(player.getPlayerType() == PlayerTypeEnum.HUMAN)
			lblPlayer.setIcon(new ImageIcon(Counter.class.getResource("/br/ucb/cm/main/mainview/resources/user_male_olive_green.png")));
		else
			lblPlayer.setIcon(new ImageIcon(Counter.class.getResource("/br/ucb/cm/main/mainview/resources/my_computer.png")));
		
		content.add(lblPlayer);
		lblPlayer.setFont(new Font("Papyrus", Font.PLAIN, 22));
		lblPlayer.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		JSeparator separator = new JSeparator();
		content.add(separator);
		separator.setMaximumSize(new Dimension(Short.MAX_VALUE, 1));
		
		JPanel panel = new JPanel();
		content.add(panel);
		panel.setOpaque(false);
		panel.setPreferredSize(new Dimension(10, 50));
		panel.setMaximumSize(new Dimension(32767, 50));
		
		JLabel lblPoints = new JLabel("Pontos:");
		
		pointsField = new JLabel("0");
		pointsField.setFont(new Font("Papyrus", Font.BOLD, 20));
		pointsField.setHorizontalAlignment(SwingConstants.CENTER);
		pointsField.setBorder(new LineBorder(new Color(0, 0, 0)));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(5)
					.addComponent(lblPoints)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pointsField, GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(6)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPoints)
						.addComponent(pointsField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(31, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		JSeparator separator_1 = new JSeparator();
		content.add(separator_1);
		separator_1.setMaximumSize(new Dimension(32767, 1));
		
		capturedPieces = new PieceCemetery(DEFAULT_MAX_WIDTH);
		content.add(capturedPieces);
		
		JPanel hidePanel = new JPanel();
		hidePanel.setBackground(Color.LIGHT_GRAY);
		hidePanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				content.setVisible(!content.isVisible());
			}
		});
		hidePanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		add(hidePanel, (getPlayer() == PlayerEnum.PLAYER_ONE) ? BorderLayout.EAST : BorderLayout.WEST);
	}

	public JLabel getPointsField() {
		return pointsField;
	}

	public void setPointsField(JLabel pointsField) {
		this.pointsField = pointsField;
	}

	public Integer getPoints() {
		return Integer.parseInt(getPointsField().getText());
	}
	
	public void setPoints(Integer points) {
		getPointsField().setText(points.toString());
	}
	
	public PlayerEnum getPlayer() {
		return player;
	}

	public void setPlayer(PlayerEnum player) {
		this.player = player;
	}
	
	public void capturePiece(PieceConfig piece){
		capturedPieces.addPiece(piece);
		content.revalidate();
	}

	public void setTurnOn(boolean value){
		if(value)
			content.setBackground(YELLOW);
		else
			content.setBackground(null);
	}

	public void victory() {
		content.setBackground(GREEN);
	}
	
	public void defeat() {
		content.setBackground(RED);
	}
	
	public void tie() {
		content.setBackground(YELLOW);
	}

}
